<template>
  <q-layout>
    <q-layout-header ref="header">
      <xheader :title="title"></xheader>
    </q-layout-header>
    <q-page-container>
      <xscrollarea :scroll-height="scrollHeight">
        <q-page padding>
          <q-input ref="code" float-label="随工单号" v-model="code" clearable
                   @blur="searchBatchInfo"
                   @keydown.enter="searchBatchInfo" :after="$parent.getScanIcon('code',this)">
            <q-autocomplete
              @search="$parent.autoSearch"
              :min-characters="1"
              :max-results="99"
            />
          </q-input>
          <q-input float-label="随工单信息" placeholder="请输入随工单号" readonly v-model="codeInfo" type="textarea" rows="2"
                   hide-underline/>
          <q-input ref="facCode" float-label="设备编码" v-model="facCode" clearable
                   @blur="getMaterialInfo" :after="$parent.getScanIcon('facCode',this)"
                   @keydown.enter="getMaterialInfo"/>
          <q-input float-label="设备信息" placeholder="请输入设备编码" readonly v-model="facInfo" type="textarea" rows="3"
                   hide-underline/>
          <q-input placeholder="备注" v-model="description" type="textarea" rows="3"/>
          <br>
          <q-btn label="提交" color="primary" class="full-width" @click="submit"/>
        </q-page>
      </xscrollarea>
    </q-page-container>
  </q-layout>
</template>

<script>

import utils from '../../js/utils'
import Xscrollarea from '../../components/xscrollarea'
import Xheader from '../../components/xheader'

export default {
  name: 'manu_data_collection',
  components: { Xheader, Xscrollarea },
  props: {
    title: {}
  },
  data: () => (
    {

      scrollHeight: 0,
      code: '',
      codeInfo: '',
      facCode: '',
      facInfo: '',
      description: '',
      batchId: '',
      fCode: '',
      pageCode: '',
      rework: '',
      subbool: false,
      facIsNotNull: false,
      pageSet: {},
      dcBtnList: []
    }),
  methods: {
    submit () {
      if (!this.batchId) {
        this.$parent.info('随工单号不规范！')
        return
      }
      if (facIsNotNull && !this.fCode) {
        this.$parent.info('设备编码不能为空！')
        return
      }
      if (this.subbool) {
        return
      }
      this.subbool = true
      this.axios.post(this.$conf.server + '/manuBatch/saveManuProcess',
        this.$qs.stringify({
          id: this.batchId,
          facCode: this.fCode,
          description: this.description,
          code: this.pageCode,
          isRework: this.rework
        })).then(res => {
        this.subbool = false
        if (res.data.msg === 'rework') {
          if (this.$parent.confirm('“' + pageSet.NAME + '” 工序之前已执行，是否再次执行“' + pageSet.NAME + '”工序？')) {
            submitData('true')
          }
          return
        }
        this.$parent.info(res.data.msg)
        if (res.data.result) {
          window.location.reload()
        }
      })
    },
    searchBatchInfo () {
      if (this.code === '' || this.pageCode === '') {
        return
      }
      this.axios.post(this.$conf.server + '/manuBatch/searchBatchInfo',
        this.$qs.stringify({ code: this.code, pageCode: this.pageCode })).then(res => {
        if (res.data.result) {
          this.batchId = res.data.ID
          this.codeInfo = '规格：' + utils.nvl(res.data.CUT_METHOD) +
              (res.data.TYPENAME ? '\r\n类型：' + res.data.TYPENAME : '') +
              (res.data.PNAME ? '\r\n产品名称：' + res.data.PNAME : '') +
              (res.data.PFNAME ? '\r\n配方名称：' + res.data.PFNAME + '(' + res.data.VERSION + ')' : '') +
              (res.data.IS_TEST === 'Y' ? "\r\n<span style='color: #f00;'>试验件</span>" : '') +
              (res.data.DESCRIPTION === 'Y' ? res.data.DESCRIPTION : '')

          for (let v of vm.Dc_Btn_List)
          // 查询每个数据采集按钮的历史数据
          {
            getDataHistroy(v.EMES_DC_INPUT_DATA_ID)
          }
        } else {
          this.$parent.info(res.data.msg)
        }
      })
    },
    getMaterialInfo () {
      if (this.facCode === '') {
        return
      }
      this.axios.post(this.$conf.server + '/manuBatch/getFacilitiesInfo', this.$qs.stringify({ code: this.facCode })).then(res => {
        if (res.data.result) {
          this.facCode = this.code
          this.facInfo = utils.nvl(res.data.NAME)
        } else {
          this.$parent.info(res.data.msg)
        }
      })
    },
    initPageCode () {
      this.pageCode = this.$route.query.code
      if (!this.pageCode) {
        this.$parent.info('未搜索到传入的页面编码，请联系管理员！')
        return
      }
      this.axios.post(this.$conf.server + '/manuBatch/getManuPageInfo',
        this.$qs.stringify({ code: this.pageCode })).then(res => {
        if (res.data.result) {
          if (res.data.joinFac === 'Y') {
            $('#facInput').show()
            $('#facInputInfo').show()

            facIsNotNull = true
          }

          this.pageSet = res.data
          this.dcBtnList = res.data.Dc_Btn_List

          // 请求到后台，判定当前设备是否有在运行中的随工单
          this.axios.post(this.$conf.server + '/manuBatch/getManuBatchInfoByIp', this.$qs.stringify({ code: this.pageCode }))
            .then(res => {
              if (res.data.result) {
                if (res.data.BATCHCODE) {
                  this.code = res.data.BATCHCODE
                  this.searchBatchInfo()
                }
                if (res.data.CODE && facIsNotNull) {
                  this.facCode = res.data.CODE
                  this.getMaterialInfo()
                }
              }
            })
        } else {
          this.$parent.info(res.data.msg)
        }
      })
    },
    manuDataDc (pageId, type, url) {
      if (type === 'DC') { // 数采
        this.axios.post(this.$conf.server + '/manuFacilitiesDataController/getData',
          this.$qs.stringify({
            pageId: pageId,
            batchId: this.batchId,
            processId: this.pageSet.EMES_PROCESS_ID
          })).then(res => {
          this.$parent.info(res.data.msg)
          if (res.data.result) {
            getDataHistroy(pageId)
          }
        })
      } else if (type === 'DIALOG') { // 对话框
        top.openUrl(url, '', '80%', '80%', window)
      }
    }
  },
  mounted () {
    this.scrollHeight = this.$parent.webHeight - this.$refs.header.$el.clientHeight
  },
  created () {
    this.initPageCode()
  }
}
</script>

<style scoped>

</style>
